package form;

import database.DBO;
import widget.*;

import javax.swing.*;
import java.awt.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;

/**
 * Created by LLAP on 2017/12/25.
 * Copyright (c) 2017 LLAP. All rights reserved.
 */
public class DemandSearch extends Form {

    public DemandSearch() {
        super("求购/求租客户检索", 800, 600);
        this.setMaxWidth(800);
        JButton confirm = new JButton("Confirm");
        QueryTable table = new QueryTable();
        table.addRow().add(new TLabel("求购/求租查询"));
        table.addRow().add(new TTextField("name", "客户姓名"));
        table.addRow().add(new TRadioGroup("gender", new Object[] {"男", "女"}, "性别"));
        table.addRow().add(new TTextField("phone", "联系电话"));
        table.addRow().add(new TLabel("求购/求租意向"));
        table.addRow().add(new TRadioGroup("demand", new Object[] {"求购", "求租"}, "意向"));
        table.addRow().add(new TMultipleSelect("type", new Object[] {"平层", "复式", "跃式"}, "户型"));
        table.addRow().add(new TMultipleSelect("decor", new Object[] {"精装", "简装", "毛坯"}, "装修"));
        table.addRow().setHeight(100).add(
                new TMultipleSelect("direction", new Object[] {"东", "西", "南", "北"}, "房屋朝向")
        );
        table.addRow().add(new TTextField("area", "面积（平方米）"));
        table.addRow().add(new TTextField("price", "底价（万元）"));
        this.add("Center", table.pack());
        this.add("South", confirm);

        confirm.addActionListener(e -> {
            try {
                HashMap<String, Object> data = table.getData();
                LinkedList<Object> args = new LinkedList<>();
                //System.out.println(data);
                DBO db = new DBO();
                String sql = "select * from demand where did > 0 ";
                if (!data.get("demand").equals("")) {
                    sql += " and demand = ? ";
                    args.add(data.get("demand"));
                }
                if (!data.get("area").equals("")) {
                    sql += " and area >= ? ";
                    args.add(data.get("area"));
                }
                if (!data.get("price").equals("")) {
                    sql += " and price >= ? ";
                    args.add(data.get("price"));
                }
                if (((ArrayList<String>)data.get("type")).size() > 0) {
                    sql += " and did in (select distinct demand_type.did from demand_type where type in (";
                    for (String i : (ArrayList<String>)data.get("type")) {
                        sql += "\'" + i + "\',";
                    }
                    sql += "\'\')) ";
                }
                if (((ArrayList<String>)data.get("decor")).size() > 0) {
                    sql += " and did in (select distinct demand_decor.did from demand_decor where decor in (";
                    for (String i : (ArrayList<String>)data.get("decor")) {
                        sql += "\'" + i + "\',";
                    }
                    sql += "\'\')) ";
                }
                if (((ArrayList<String>)data.get("direction")).size() > 0) {
                    sql += " and did in (select distinct demand_direction.did from demand_direction where direction in (";
                    for (String i : (ArrayList<String>)data.get("direction")) {
                        sql += "\'" + i + "\',";
                    }
                    sql += "\'\')) ";
                }

                System.out.println(sql);
                //if (true) return;
                PreparedStatement statement = db.prepare(sql);
                int i = 1;
                for (Object a : args) {
                    if (a != null) statement.setString(i++, a.toString());
                    else statement.setNull(i++, Types.NULL);
                }
                ResultSet result = statement.executeQuery();
                new DemandResult(result).setVisible(true);
            }
            catch (Exception ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage());
                ex.printStackTrace();
            }
        });
    }

    public static void main(String[] args) {
        new DemandSearch().setVisible(true);
    }
}
class DemandResult extends Form {
    public DemandResult(ResultSet r) {
        super("求租/求购搜索结果", 800, 500);
        this.setMaxWidth(800);
        DataTable table = new DataTable(new entity.Demand(r));
        this.setLayout(new BorderLayout(10, 10));
        this.add("Center", table);
    }
}